<?php

namespace App\Store\Controllers\Goods;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\Store\Common as StoreCommon;


use App\Common\Services\CommonExcelExport;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Goods\GoodsStockin;
use App\Store\Models\Goods\GoodsStockinInfolist;
use App\Store\Models\Goods\GoodsStockList;
use Maatwebsite\Excel\Facades\Excel;
use App\Store\Services\Excel\DownloadTemp;
use Illuminate\Support\Facades\DB;
use App\Store\Validate\Goods\GoodsStockin as ValidateGoodsGoodsStockin;

/**
 * 商品入库列表明细
 */
class GoodsStockinInfolistController extends Controller
{
    /**
     * 商品入库
     */
    public function getlist(Request $request)
    {

        $list = GoodsStockinInfolist::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }


    /**
     * 导出
     */
    public function doexport(Request $request)
    {
        $list = GoodsStockinInfolist::getList($request)['list'];
        $row = [
            ['start_store'=>'商品入库列表明细'],
            [
            'startstore' => '起始门店',
            'targetstore' => '目标门店',
            'order_sncode' => '订单号',
            'paymoney' => '应付账款',
            'brand_name' => '品牌',
            'audit_status' => '状态',
            'type' => '类型',
            'category_name' => '入库类别',
            'category_name1' => '商品类别',
            'goods_sn_code' => '条码',
            'goods_name' => '品名',
            'label_price' => '标签价',
            'label_price_new' => '入库标签价',
            'piece_weight' => '件重',
            'piece_weight_new' => '入库件重',
            'netglod_weight' => '金重',
            'netglod_weight_new' => '入库金重',
            'total_piece_weight' => '总重量',
            'total_piece_weight_new' => '入库总重量',
            'gramprice_fee' => '克销工费',
            'pieceprice_fee' => '件销工费',
            'count' => '数量',
            'cf_no' => '证书号',
            'main_stone_weigth' => '主石重',
            'main_stone_count' => '主石数',
            'main_stone_val' => '主石值',
            'other_stone_weigth' => '副石重',
            'other_stone_count' => '副石数',
            'other_stone_val' => '副石值',
            'gemstone_fee1' => '宝石费1',
            'gemstone_fee2' => '宝石费2',
            'color' => '颜色',
            'clarity' => '净度',
            'is_code_more' => '一码多货',
            'create_at' => '创建时间',
        ]];

        $is_store_account = StoreCommon::getIsStoreAccount($request);
        if (!$is_store_account) {
            unset($row[0]['supply_name']);
            unset($row[0]['paymoney']);
            unset($row[0]['costglod_price']);
            unset($row[0]['total_fee']);
            unset($row[0]['piececost_fee']);
            unset($row[0]['piececost_fee_total']);
        }

        $exceldata = [];
        foreach ($list as $val) {
            $maininfo = GoodsStockin::where(['cp_id' => $val['cp_id'], 'id' => $val['p_id']])->select(['order_sncode', 'create_at'])->first();
            $exceldata[] = [
                'startstore' => '财务部',
                'targetstore' => $val['storeinfo']['store_name'],
                'order_sncode' => $maininfo->order_sncode,
                'paymoney' => $val['paymoney'] / 100,
                'brand_name' => isset($val['goods_brand']['brand_name']) ? $val['goods_brand']['brand_name'] : '',
                'audit_status' => $val['audit_status'] == 1 ? '待审核' : ($val['audit_status'] == 2 ? '审核' : '取消'),
                'type' => '入库',
                'category_name' => $val['goods_category']['category_name'],
                'category_name1' => $val['goods_category']['category_name'],
                'goods_sn_code' => $val['goods_sn_code'],
                'goods_name' => $val['goods_name'],
                'label_price' => $val['label_price'] / 100,
                'label_price_new' => $val['label_price_new'] / 100,
                'piece_weight' => $val['piece_weight'],
                'piece_weight_new' => $val['piece_weight_new'],
                'netglod_weight' => $val['netglod_weight'],
                'netglod_weight_new' => $val['netglod_weight_new'],

                'total_piece_weight' => $val['total_piece_weight'],
                'total_piece_weight_new' => $val['total_piece_weight_new'],

                'gramprice_fee' => $val['gramprice_fee'] / 100,
                'pieceprice_fee' => $val['pieceprice_fee'] / 100,
                'count' => $val['count'],
                'cf_no' => $val['cf_no'],
                'main_stone_weigth' => $val['main_stone_weigth'],
                'main_stone_count' => $val['main_stone_count'],
                'main_stone_val' => $val['main_stone_val'],
                'other_stone_weigth' => $val['other_stone_weigth'],
                'other_stone_count' => $val['other_stone_count'],
                'other_stone_val' => $val['other_stone_val'],

                'gemstone_fee1' =>$val['gemstone_fee1'] / 100,
                'gemstone_fee2' => $val['gemstone_fee2'] / 100,
                'color' => $val['color'],
                'clarity' => $val['clarity'],
                'is_code_more' => $val['is_code_more'] == 1 ? '否' : '是',
                'create_at' => date('Y-m-d', $maininfo->create_at),


                // 'stockin_at' => date('Y-m-d', $val['stockin_at']),
                // 'supply_name' => isset($val['supply']['supply_name'])?$val['supply']['supply_name']:'',
                // 'style_name' => isset( $val['goods_style']['style_name'])? $val['goods_style']['style_name']:'',
                // 'content' => $val['content'],
                // 'costglod_price' => $val['costglod_price'] / 100,
                // 'costpiece_fee' => $val['costpiece_fee'] / 100,
                // 'costgram_fee' => $val['costgram_fee'] / 100,
                // 'piececost_fee' => $val['piececost_fee'] / 100,
                // 'goods_craft_name' => isset($val['goods_craft']['craft_name'])?$val['goods_craft']['craft_name']:'',
                // 'c_no' => $val['c_no'],
                // 's_no' => $val['s_no'],
                // 'gemstone_fee1' => $val['gemstone_fee1'] / 100,
                // 'gemstone_fee2' => $val['gemstone_fee2'] / 100,
                // 'add_fee1' => $val['add_fee1'] / 100,
                // 'add_fee2' => $val['add_fee2'] / 100,
                // 'cf_no_gia' => $val['cf_no_gia'],
                // 'cf_fee' => $val['cf_fee'] / 100,
                // 'cut' => $val['cut'],
                // 'goods_pricetype_name' => isset($val['goods_pricetype']['goods_pricetype_name'])?$val['goods_pricetype']['goods_pricetype_name']:'',
                // 'goods_stone_name' => isset($val['goods_stone']['goods_stone_name'])?$val['goods_stone']['goods_stone_name']:'',
                // 'procure_at' => $val['procure_at']>0?date('Y-m-d', $val['procure_at']):'',
                // 'paytype_name' => isset($val['paytype']['paytype_name'])?$val['paytype']['paytype_name']:'',
                // 'remark' => $val['remark'],
            ];
        }

        $filename = '商品入库列表明细导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }
}
